package appnode;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import javax.jdo.Query;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import appnode.Node;
import appnode.RateUtils;
import appnode.NodeRating;
import appnode.Ratings;
import appnode.SearchNode;
import appnode.PMF;
import java.util.Date;

public class AddRating extends HttpServlet {
		
	    public void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws IOException {
			UserService userService = UserServiceFactory.getUserService();
	        User user = userService.getCurrentUser();
			String u = user.getNickname();
			PersistenceManager pm1 = PMF.get().getPersistenceManager();
			String nodeowner = req.getParameter("nodeowner");
			String nodertowner = req.getParameter("nodertowner");
			String noderating = req.getParameter("noderating");
			String nodename = req.getParameter("nodename");
			String com = req.getParameter("com");
			String col = req.getParameter("col");
			String exp = req.getParameter("exp");
			
			
					
			// get node
			Node nodee = SearchNode.getByName(nodename);
			Key key = KeyFactory.stringToKey(nodee.getKeyStr());
			Node node = pm1.getObjectById(Node.class,key);
			
			//rate
			double comrating = RateUtils.stringtoRate(com);
			double colrating = RateUtils.stringtoRate(col);
			double exprating = RateUtils.stringtoRate(exp);
			
			// add noderating
			int i = 0;
			int j = 0;
			int index = 0;
			for (i=0;i<node.getNoderatings().size();i++) {
				if (node.getNoderatings().get(i).getNodename().equals(noderating)) {
					j++;
					index = i;
				}
			}
			if (j>0) {
				node.getNoderatings().get(index).setCommun(RateUtils.addComrate(nodename,noderating,comrating));
				node.getNoderatings().get(index).setCollabor(RateUtils.addColrate(nodename,noderating,colrating));
				node.getNoderatings().get(index).setExpert(RateUtils.addExprate(nodename,noderating,exprating));
				pm1.close();
				
			}
			else {
				NodeRatings noderatings = new NodeRatings(noderating,nodertowner,RateUtils.addComrate(nodename,noderating,comrating),RateUtils.addColrate(nodename,noderating,colrating),RateUtils.addExprate(nodename,noderating,exprating),new Date());
				node.getNoderatings().add(noderatings);
			}		
			
			// add ownerrating
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery(Ratings.class);
			query.setFilter("owner==nameParam");
			query.declareParameters("String nameParam");
			List<Ratings> results = (List<Ratings>) query.execute(nodertowner);
			if (results.isEmpty()) {
				Ratings rating = new Ratings(nodertowner,RateUtils.addComrateOwner(nodertowner),RateUtils.addColrateOwner(nodertowner),RateUtils.addExprateOwner(nodertowner),new Date());
				pm.makePersistent(rating);
				rating.setNumofrate(1);
			}
			else {
				results.get(0).setCommun(RateUtils.addComrateOwner(nodertowner));
				results.get(0).setCollabor(RateUtils.addColrateOwner(nodertowner));
				results.get(0).setExpert(RateUtils.addExprateOwner(nodertowner));
				results.get(0).setNumofrate(results.get(0).getNumofrate()+1);
			}
			pm.close();
			resp.setContentType("text/html");
			resp.getWriter().println("<html>");
			resp.getWriter().println("<div style='font-family:Verdana;font-size: smaller;'>You add ratings succesfully!</div>");
			resp.getWriter().println("<br>");
			resp.getWriter().println("<input type='button' value='Ok' onClick='self.close()'/>");
			resp.getWriter().println("</html>");
		}			
}